﻿Public Class FormCategoriaListado
    Dim classController As New Class_RC_Controller.rc_Empleados()

    Private m_frmCategoria As FrmCategoria
    Public Property frmCategoria() As FrmCategoria
        Get
            Return m_frmCategoria
        End Get
        Set(ByVal value As FrmCategoria)
            m_frmCategoria = value
        End Set
    End Property
    Private Sub FormCategoriaListado_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        dgCategoria.AutoGenerateColumns = False
        CargarCategoria()
        llenarTipoEmp()
    End Sub
    Private Sub CargarCategoria()
        Dim codtipoemp As Integer
        If cbxTipoEmp.Checked Then
            codtipoemp = cmbtipoemp.SelectedValue
        End If
        Dim dt As New DataTable
        dt = classController.get_Categoria_All(oiml.dbConn, 0, txtDescripcion.Text, codtipoemp)
        dgCategoria.DataSource = dt
    End Sub
    Private Sub llenarTipoEmp()
        Dim dsTipoEmp As New DataTable
        dsTipoEmp = classController.get_TipoEmpleado_All(oiml.dbConn, 0, "")
        If Not dsTipoEmp Is Nothing Then
            If dsTipoEmp.Rows.Count > 0 Then
                cmbtipoemp.DataSource = dsTipoEmp
                cmbtipoemp.DisplayMember = "DescTipoEmp"
                cmbtipoemp.ValueMember = "CodTipoEmp"
            End If
        End If
    End Sub

    Private Sub btnEditar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEditar.Click
        Try
            If dgCategoria.SelectedRows.Count > 0 Then
                Dim CodCategoria As New Integer
                CodCategoria = dgCategoria.SelectedRows(0).Cells(0).Value

                If frmCategoria Is Nothing Then
                    frmCategoria = New FrmCategoria
                    frmCategoria.codCategoria = CodCategoria
                    'If frmServicios.ShowDialog() = Windows.Forms.DialogResult.OK Then
                    frmCategoria.ShowDialog()
                    CargarCategoria()
                    frmCategoria.Dispose()
                    frmCategoria = Nothing
                    'End If
                Else
                    frmCategoria.codCategoria = CodCategoria
                    frmCategoria.ShowDialog()
                End If
            End If
        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub btnCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelar.Click
        Me.Close()

    End Sub

    Private Sub txtBuscar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtBuscar.Click
        CargarCategoria()
    End Sub

    Private Sub txtDescripcion_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtDescripcion.KeyDown
        If e.KeyCode = Keys.Enter Then
            CargarCategoria()
        End If
    End Sub
    Private Sub dgCategoria_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgCategoria.DoubleClick
        btnEditar_Click(sender, e)
    End Sub

    Private Sub dgCategoria_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles dgCategoria.KeyDown
        If e.KeyCode = Keys.Enter Then
            btnEditar_Click(sender, e)
        End If

    End Sub

    Private Sub btnNuevo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNuevo.Click
        '    If frmciudad Is Nothing Then
        frmCategoria = New FrmCategoria()
        frmCategoria.ShowDialog()
        CargarCategoria()
    End Sub
End Class